<?php
class PageDataProvider implements IDataProvider {
	/*
	 * (non-PHPdoc) @see IDataProvider::provide()
	 */
	public function provide($params) {
		global $_PAGE;
		static $pM = false, $cM = false;
		$page_id = $_PAGE ['page_id'];
		unset ( $params ['params'] );
		extract ( $params );
		if (! $pM) {
			$pM = new WebPageModel ();
			$cM = new WebPageCategory ();
		}
		// 要查询的字段
		$fields = 'WP.*,WPC1.name as category_name,WPC1.url AS category_url,WPC2.name AS subcategory_name,WPC2.url AS subcategory_url,WPC1.bind AS bind,WPC1.home as domain_home';
		$fields .= apply_filter ( 'get_cts_page_fields', '', $params );
		// 条件
		$where = " WP.deleted = 0 AND WP.status = 'published' ";
		// 查询表
		$from = ' ((web_page AS WP LEFT JOIN web_page_category AS WPC1 ON (WP.category = WPC1.name)) LEFT JOIN web_page_category AS WPC2 ON (WP.subcategory = WPC2.name))';
		$from .= apply_filter ( 'get_cts_page_from', '', $params );
		
		if (isset ( $category ) && ! empty ( $category )) { // 读取主分类,属性:category
			$category = explode ( ",", $category );
			$where .= " AND WP.category IN ('" . implode ( "','", $category ) . "')";
		} else if (isset ( $categories )) { // 读取当前分类内容，包括该分类的子分类 。属性:categories
			$categories = $cM->getChildCateNames ( $categories );
			if (! empty ( $categories )) {
				$where .= " AND WP.category IN ('" . implode ( "','", $categories ) . "')";
			}
		}
		
		if (isset ( $subcategory ) && ! empty ( $subcategory )) { // 读取主分类
			$subcategory = explode ( ",", $subcategory );
			$where .= " AND WP.subcategory IN ('" . implode ( "','", $subcategory ) . "')";
		} else if (isset ( $subcategories ) && ! empty ( $subcategories )) { // 读取当前分类内容，包括该分类的子分类
			$subcategories = $cM->getChildCateNames ( $subcategories );
			if (! empty ( $subcategories )) {
				$where .= " AND WP.subcategory IN ('" . implode ( "','", $subcategories ) . "')";
			}
		}
		
		// 内容模型,属性:models = 'aaa,bbb,ccc'
		if (isset ( $models ) && ! empty ( $models )) {
			$models = explode ( ",", $models );
			$where .= " AND WP.page_mode IN ('" . implode ( "','", $models ) . "')";
		}
		// 页面类型,属性:types = 'aaa,bbb,ccc'
		if (isset ( $types ) && ! empty ( $types )) {
			$types = explode ( ",", $types );
			$where .= " AND WP.page_type IN ('" . implode ( "','", $types ) . "')";
		}else{
			$where .= " AND WP.page_type <> 'template' ";
		}
		// 置顶,属性：ontop = 1
		if (isset ( $ontop ) && $ontop) {
			$where .= " AND WP.ontopto >= '" . date ( 'Y-m-d' ) . "'";
		}
		// 有插图，属性:figure = 1
		if (isset ( $figure ) && $figure) {
			$where .= " AND WP.figure <> ''";
		}
		//作者,属性 : author = 'aaa,bbb,ccc'
		if (isset ( $author ) && ! empty ( $author )) {
			$author = explode ( ",", $author );
			$where .= " AND WP.author IN ('" . implode ( "','", $author ) . "')";
		}
		//来源，属性：source = 'aaa,bbb,ccc'
		if (isset ( $source ) && ! empty ( $source )) {
			$source = explode ( ",", $source );
			$where .= " AND WP.source IN ('" . implode ( "','", $source ) . "')";
		}
		//扩展查询条件
		$where .= apply_filter ( 'get_cts_page_where', '', $params );
		// 属性,属性:flags = 'aa,bbb,ccc' or flags = 'aaa|bbb|ccc'
		if (isset ( $flags ) && ! empty ( $flags )) {
			if (strpos ( $flags, "|" ) > 0) { // 且
				$flags = explode ( '|', $flags );
				foreach ( $flags as $flag ) {
					$where .= " AND EXISTS (SELECT WPT.tag_id FROM web_page_tag as WPT LEFT JOIN web_tag AS WT ON(WPT.tag_id = WT.tag_id) WHERE WPT.page_id = WP.page_id AND WT.type = 'flag' AND WT.tag = '" . $flag . "')";
				}
			} else {
				$flags = explode ( ',', $flags );
				$flags = implode ( "','", $flags );
				$where .= " AND EXISTS (SELECT WPT.tag_id FROM web_page_tag as WPT LEFT JOIN web_tag AS WT ON(WPT.tag_id = WT.tag_id) WHERE WPT.page_id = WP.page_id AND WT.type = 'flag' AND WT.tag IN ('" . $flags . "'))";
			}
		}
		// 标签，属性：tags = 'aaaa,bbb,ccc' or tags = 'aaa|bbb|ccc'
		if (isset ( $tags ) && ! empty ( $tags )) {
			if (strpos ( $tags, "|" ) > 0) { // 且
				$tags = explode ( '|', $tags );
				foreach ( $tags as $tag ) {
					$where .= " AND EXISTS (SELECT WPT.tag_id FROM web_page_tag as WPT LEFT JOIN web_tag AS WT ON(WPT.tag_id = WT.tag_id) WHERE WPT.page_id = WP.page_id AND WT.type = 'tag' AND WT.tag = '" . $tag . "')";
				}
			} else { // 或
				$tags = explode ( ',', $tags );
				$tags = implode ( "','", $tags );
				$where .= " AND EXISTS (SELECT WPT.tag_id FROM web_page_tag as WPT LEFT JOIN web_tag AS WT ON(WPT.tag_id = WT.tag_id) WHERE WPT.page_id = WP.page_id AND WT.type = 'tag' AND WT.tag IN ('" . $tags . "'))";
			}
		}
		// 分页查询
		$totalCount = 0;
		if (isset ( $paging ) && $paging) { // 需要分页
			$countSQL = "SELECT COUNT(WP.page_id) AS total FROM {$from} WHERE {$where}";
			$ctotal = $pM->query ( $countSQL );
			if ($ctotal) {
				$totalCount = $ctotal [0] ['total'];
			}
		}
		
		$sql = "SELECT {$fields} FROM {$from} WHERE {$where} ";
		// 当设置 random = 1时取随机网页 
		if (isset ( $random ) && ! empty ( $random )) {
			$sql .= " ORDER BY RAND() ";
		} else {
			// 排序,属性 sort = 
			if (isset ( $sort ) && ! empty ( $sort )) {
				$sql .= " ORDER BY {$sort} ";
			} else {
				$sql .= " ORDER BY WP.publish_time DESC ";
			}
		}
		// 分页
		if (isset ( $start )) {
			$start = intval ( $start );
			$start = $start < 1 ? 1 : $start;
		} else {
			$start = 1;
		}
		if (isset ( $limit )) {
			$limit = intval ( $limit );
			$limit = $limit < 1 ? 10 : $limit;
		} else {
			$limit = 10;
		}
		$pages = $pM->query ( $sql, array ('start' => $start - 1, 'limit' => $limit ) );
		if ($pages) {
			return new CtsDataSet ( $pages, $totalCount, $limit );
		} else {
			return new CtsDataSet ( array (), 0, $limit );
		}
	}
}